import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;

public class Generator {
	
	private int numberOfPatterns;
	private int numberOfInputUnits;
	private int numberOfOutputUnits;	

	public Generator(int numberOfPatterns, int numberOfInputUnits,
			int numberOfOutputUnits) {
		super();
		this.numberOfPatterns = numberOfPatterns;
		this.numberOfInputUnits = numberOfInputUnits;
		this.numberOfOutputUnits = numberOfOutputUnits;
	}
	
	private double function(double x, double y, double z) {
		return Math.cosh(y + z) + Math.pow(x, 2) - 1;
	}
	
	public void generate() {
		PrintWriter pw = null;			
		try {
			pw = new PrintWriter(new FileWriter(new File("my_train_data.pat")));
			pw.println(
					"SNNS pattern definition file V3.2\n\n" +
					"No. of patterns : " + numberOfPatterns + "\n" + 
					"No. of input units : " + numberOfInputUnits + "\n" +
					"No. of output units : " + numberOfOutputUnits);
			
			Double[][] inputUnits = new Double[numberOfPatterns][numberOfInputUnits];
			Double[] outputUnits = new Double[numberOfPatterns];			

			DecimalFormat df = new DecimalFormat("##0.000");
			
			for (int i = 0; i < numberOfPatterns; i++) {
					
				for (int j = 0; j < numberOfInputUnits; j++) {
					inputUnits[i][j] = Math.random() + 1;;
				}
				
				outputUnits[i] = function(inputUnits[i][0], inputUnits[i][1], inputUnits[i][2]);
			}
			
			double max = Collections.max(Arrays.asList(outputUnits));
			for (int i = 0; i < numberOfPatterns; i++) {
				outputUnits[i] = outputUnits[i] / max;	
			}
			
			for (int i = 0; i < numberOfPatterns; i++) {
				pw.println("# input pattern " + i);	
				for (int j = 0; j < numberOfInputUnits; j++) {
					pw.print(" " + df.format(inputUnits[i][j]));
				}
				pw.println();
				pw.println("# output pattern " + i);
				pw.println(" " + df.format(outputUnits[i]));
			}
			
			pw.flush();
		} catch (IOException e) {
			System.out.println("Subor nebol najdeny");
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.close();
			}
		}
	}
}